<?php

namespace App\Http\Middleware;


use App\Models\Department;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;
use Symfony\Component\Routing\Tests\Generator\UrlGeneratorTest;
use Closure;

class CheckDomain
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if( session()->get('doain')==null){
            $url = preg_replace("/(lara4.com).+/", "lara4.com", url()->current(),1);
//        $url = preg_replace("/^(lara4.com)\.?/", "lara4.com", url()->current(),1);
            $url = preg_replace("/http:\/\//", "", $url,1);
            $depart = Department::where(['domain'=>$url,'active'=>'0'])->first();
            if(!$depart) return back()->with('errors','此域名不存在');
            Session::put('doain', $depart);
//            dd(Session::get('domain'));
            session()->put('doain',$depart);
        }
        return $next($request);
    }
}


